<!-- src/views/account/Password.vue -->
<template>
  <div class="password-container">
    <h2>修改密码</h2>
    <el-card class="password-card">
      <el-form
          :model="passwordForm"
          :rules="passwordRules"
          ref="passwordFormRef"
          label-width="120px"
          @submit.prevent="changePassword"
      >
        <el-form-item label="原密码" prop="oldPassword">
          <el-input
              v-model="passwordForm.oldPassword"
              type="password"
              show-password
              placeholder="请输入原密码"
          />
        </el-form-item>

        <el-form-item label="新密码" prop="newPassword">
          <el-input
              v-model="passwordForm.newPassword"
              type="password"
              show-password
              placeholder="请输入新密码"
          />
        </el-form-item>

        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input
              v-model="passwordForm.confirmPassword"
              type="password"
              show-password
              placeholder="请再次输入新密码"
          />
        </el-form-item>

        <el-form-item>
          <el-button
              type="primary"
              @click="changePassword"
              :loading="changing"
              :disabled="changing"
          >
            {{ changing ? '修改中...' : '修改密码' }}
          </el-button>
          <el-button @click="resetForm" style="margin-left: 10px;">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { UserApi } from '@/api/user/index.js'

// 表单数据
const passwordForm = reactive({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

// 表单引用
const passwordFormRef = ref(null)

// 修改状态
const changing = ref(false)

// 表单验证规则（已移除密码长度限制）
const passwordRules = {
  oldPassword: [
    { required: true, message: '请输入原密码', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== passwordForm.newPassword) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

// 修改密码
const changePassword = async () => {
  if (!passwordFormRef.value) return

  await passwordFormRef.value.validate(async (valid) => {
    if (valid) {
      changing.value = true
      try {
        // 准备请求数据，字段名与后端 UpdatePasswordDTO 匹配
        const requestData = {
          oldPassword: passwordForm.oldPassword,
          newPassword: passwordForm.newPassword
        }

        const res = await UserApi.updateUserPassword(requestData)
        ElMessage.success('密码修改成功，请重新登录')

        // 重置表单
        resetForm()

        // 可以在这里添加重新登录逻辑或跳转到登录页
        // 例如: router.push('/login')
      } catch (error) {
        ElMessage.error(error.message || '密码修改失败')
      } finally {
        changing.value = false
      }
    } else {
      ElMessage.error('请检查表单填写是否正确')
    }
  })
}

// 重置表单
const resetForm = () => {
  if (passwordFormRef.value) {
    passwordFormRef.value.resetFields()
  }
}
</script>

<style scoped>
.password-container {
  max-width: 600px;
  margin: 20px auto;
  padding: 20px;
}

.password-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.password-container h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
}

:deep(.el-form-item__content) {
  display: flex;
  align-items: center;
}

:deep(.el-button) {
  min-width: 100px;
}
</style>
